package com.lhq.tag_tales.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lhq.tag_tales.common.BaseResponse;
import com.lhq.tag_tales.common.ErrorCodeEnum;
import com.lhq.tag_tales.exception.BusinessException;
import com.lhq.tag_tales.model.domain.Team;
import com.lhq.tag_tales.model.domain.dto.TeamQuery;
import com.lhq.tag_tales.model.domain.request.TeamAddRequest;
import com.lhq.tag_tales.model.domain.request.TeamJoinRequest;
import com.lhq.tag_tales.model.domain.request.TeamUpdateRequest;
import com.lhq.tag_tales.model.domain.vo.TeamUserVO;
import com.lhq.tag_tales.service.TeamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户接口
 */
@Slf4j
@RestController
@RequestMapping("/team")
public class TeamController {
    @Resource
    private TeamService teamService;

    /**
     * 添加队伍
     * @param teamAddRequest 队伍添加请求对象，包含需要添加的队伍信息
     * @return 返回一个BaseResponse对象，包含添加成功的队伍ID
     * @throws BusinessException 当参数为空时抛出业务异常
     */
    @PostMapping("/add")
    BaseResponse<Long> addTeam(@RequestBody TeamAddRequest teamAddRequest){
        Long teamId = teamService.addTeam(teamAddRequest);
        return BaseResponse.success(teamId);
    }

    /**
     * 根据ID删除队伍
     * @param id 需要删除的队伍的ID
     * @return 返回一个BaseResponse对象，表示删除操作是否成功
     * @throws BusinessException 当ID参数为空或队伍不存在时抛出业务异常
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTeamById(@RequestParam Long id) {
        if (id<=0){
            throw new BusinessException(ErrorCodeEnum.PARAMS_ERROR,"参数为空");
        }
        //删除队伍
        teamService.deleteTeamById(id);
        return BaseResponse.success(true);
    }

    /**
     * 更新队伍信息
     * @param teamUpdateRequest 队伍更新请求对象，包含需要更新的队伍信息
     * @return 返回一个BaseResponse对象，表示更新操作是否成功
     * @throws BusinessException 当更新请求对象为空时抛出业务异常
     */
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest) {
        //更新队伍信息
        teamService.updateTeam(teamUpdateRequest);
        return BaseResponse.success(true);
    }

    /**
     * 根据ID获取队伍信息
     * @param id 需要获取信息的队伍的ID
     * @return 返回一个BaseResponse对象，包含队伍信息
     * @throws BusinessException 当ID参数非法或队伍不存在时抛出业务异常
     */
    @GetMapping("/get")
    public BaseResponse<Team> getTeamById(Long id){
        if (id<=0){
            throw new BusinessException(ErrorCodeEnum.PARAMS_ERROR,"参数非法");
        }
        Team team=teamService.getById(id);
        if (team==null){
            throw new BusinessException(ErrorCodeEnum.NULL_ERROR,"队伍不存在");
        }
        return BaseResponse.success(team);
    }

    /**
     * 搜索队伍
     * @param teamQuery 队伍查询对象，包含查询条件
     * @return 返回一个BaseResponse对象，包含查询到的队伍列表
     * @throws BusinessException 当查询对象为空时抛出业务异常
     */
    @Transactional
    @GetMapping("/search")
    public BaseResponse<List<TeamUserVO>> searchTeams(TeamQuery teamQuery){
        List<TeamUserVO> teamList=teamService.listTeams(teamQuery);
        return BaseResponse.success(teamList);
    }

    /**
     * 分页搜索队伍
     * @param teamQuery 队伍查询对象，包含查询条件和分页信息
     * @return 返回一个BaseResponse对象，包含分页查询到的队伍列表
     * @throws BusinessException 当查询对象为空时抛出业务异常
     */
    @Transactional
    @GetMapping("/search/page")
    public BaseResponse<Page<TeamUserVO>> searchTeamsPage(TeamQuery teamQuery){
        Page<TeamUserVO> pageResult=teamService.searchTeamsPage(teamQuery);
        return BaseResponse.success(pageResult);
    }


    /**
     * 组队功能
     * @param teamJoinRequest 组队请求对象，包含需要加入的队伍信息
     * @return 组队结果
     */
    @PostMapping("/join")
    public BaseResponse<Boolean> teamUp(@RequestBody TeamJoinRequest teamJoinRequest){
        teamService.joinTeam(teamJoinRequest);
        return BaseResponse.success(true);
    }

    /**
     * 退出队伍
     * @param teamId 队伍ID
     * @return 退出结果
     */
    @PostMapping("/quit")
    public BaseResponse<Boolean> quitTeam(Long teamId){
        teamService.quitTeam(teamId);
        return BaseResponse.success(true);
    }

    /**
     * 解散队伍
     * @param teamId 队伍ID
     * @return 解散结果
     */
    @PostMapping("/disband")
    public BaseResponse<Boolean> disbandTeam(Long teamId){
        teamService.disbandTeam(teamId);
        return BaseResponse.success(true);
    }
}

